// subpkg_worker/my/bindelder/bindelder.js
import Dialog from '@vant/weapp/dialog/dialog';
import Toast from '@vant/weapp/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    userId:'',
    elderlyId:'',
    name:'',
    beadhouse:''

    
  },
  navigator(){
    wx.navigateTo({
      url: '/subpkg_worker/search/searchElder/searchElder?role=1',
    })
    console.log(11)
  },
  bindinput(e){
    this.setData({
      elderlyId:e.detail.value
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    wx.request({
      url: 'https://zhixiao.site:8080/client/nurse/query',
      data:{
        userId:wx.getStorageSync('userid')
      },
      success:(res)=>{
        var careHomeName = res.data.data.careHomeName
        this.setData({
          beadhouse:careHomeName
        })
      }
    })
      
    
    if(options.id===undefined){
      return
    }
    this.setData({
      name:options.name,
      elderlyId:options.id
    })
  },
  bind(){
    const that = this
    if(this.data.name===''){
      Toast('请先输入要绑定的老人名称！')
      return
    }
    Dialog.confirm({
      title: '标题',
      message: `确定绑定该老人吗？`,
    })
      .then(() => {
        // on confirm
        wx.request({
          url: `https://zhixiao.site:8080/msg-bind-elder/send-request?elderId=${that.data.elderlyId}&userId=${wx.getStorageSync('userid')}&careHomeName=${wx.getStorageSync('beadhouse')}`,
          method:'POST',
          success:(res)=>{
            Dialog.alert({
              message: '信息审核中',
            }).then(() => {
              // on close
             
              wx.reLaunch({
                url: '/pages/elderList/elderList',
              })
            });
          }
        })

      })
      .catch(() => {
       
        // on cancel
      });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})